package com.example.test;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TableLayout;
import android.widget.Toast;

import com.R;

import java.util.ArrayList;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * Created by Lenovo on 2018/4/20.
 */

public class AlertDialogActivity extends Activity implements View.OnClickListener {
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        LinearLayout linearLayout = (LinearLayout) getLayoutInflater().inflate(R.layout.alertdialog_layout,null);
        Button btnCodeAdd = new Button(this);
        btnCodeAdd.setText("TestCodeAdd");
        btnCodeAdd.setVisibility(View.VISIBLE);
        //android:layout_width="wrap_content"
       //android:layout_height="wrap_content"
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
        linearLayout.addView(btnCodeAdd,layoutParams);
        setContentView(linearLayout);
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btnSimpleDialog :
                simpleDialog();
                break;
            case R.id.btnSimpleItemsDialog:
                simpleItemsDialog();
                break;
            case R.id.btnSingleChoiceItemsDialog:
                singleChoiceItemsDialog();
                break;
            case R.id.btnMultiChoiceItemsDialog:
                singleMultiItemsDialog();
                break;
            case R.id.btnCustomItemsDialog:
                customItemsDialog();
                break;
            case R.id.btnCustomViewDialog:
                customViewDialog();
                break;
        }
    }

    public void simpleDialog(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("simple")
                .setMessage("对话框测试内容\n第二行内容")
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(),"单击了［确定］按钮",Toast.LENGTH_SHORT).show();
            }
        })
        .setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                Toast.makeText(getApplicationContext(),"单击了［取消］按钮",Toast.LENGTH_SHORT).show();
            }
        })
        .create()
        .show();
    }

    public void simpleItemsDialog(){
        final String[] items = {"red","green","blue"};

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("simpleItems")
                .setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(),"你选中了《"+items[which]+"》",Toast.LENGTH_SHORT).show();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(),"单击了［确定］按钮",Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(),"单击了［取消］按钮",Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();
    }


    public void singleChoiceItemsDialog(){
        final String[] items = {"red","green","blue"};
        ListAdapter listAdapter = new ArrayAdapter(getApplicationContext(),R.layout.array_item,items) ;
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("singleChoiceItems")
                .setSingleChoiceItems(items, 1,new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(),"你选中了《"+items[which]+"》",Toast.LENGTH_SHORT).show();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(),"单击了［确定］按钮",Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(),"单击了［取消］按钮",Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();
    }

    public void singleMultiItemsDialog(){
        final String[] items = {"red","green","blue"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("singleMultiItems")
                .setMultiChoiceItems(items, new boolean[]{false, true, false}, new DialogInterface.OnMultiChoiceClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which, boolean isChecked) {
                        if(isChecked) {
                            Toast.makeText(getApplicationContext(), "你选中了《" + items[which] + "》", Toast.LENGTH_SHORT).show();
                        }else{
                            Toast.makeText(getApplicationContext(), "你取消了《" + items[which] + "》", Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(),"单击了［确定］按钮",Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(),"单击了［取消］按钮",Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();
    }

    public void customItemsDialog(){
        final String[] items = {"red","green","blue"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("customItems")
                .setAdapter(new ArrayAdapter<String>(this, R.layout.array_item, items), new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(),"你选中了《"+items[which]+"》",Toast.LENGTH_SHORT).show();
                    }
                })
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(),"单击了［确定］按钮",Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(),"单击了［取消］按钮",Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();
    }

    public void customViewDialog(){

        TableLayout loginFrom = (TableLayout)getLayoutInflater().inflate(R.layout.login,null);
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("customView")
                .setView(loginFrom)
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(),"单击了［确定］按钮",Toast.LENGTH_SHORT).show();
                    }
                })
                .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Toast.makeText(getApplicationContext(),"单击了［取消］按钮",Toast.LENGTH_SHORT).show();
                    }
                })
                .create()
                .show();
    }

}
